fix(tests): prevent telemetry fetch mock pollution in parallel tests#3399
fix(tests): prevent telemetry fetch mock pollution in parallel tests#3399
Conversation
…fetch mocks The telemetry module's `_enabled` flag persists across parallel test files when `telemetry.test.ts` calls `initTelemetry()` (which deletes BUN_ENV/NODE_ENV guards). This causes `logWarn` → `captureWarning` → `sendEvent` → `fetch()` to fire unexpected calls through other tests' `global.fetch` mocks, breaking callCount-based assertions in `hetzner-cov.test.ts` and `digitalocean-token.test.ts`. Fix: - Add runtime env guard in `sendEvent()` so telemetry never fires in test env - Set `SPAWN_TELEMETRY=0` in test preload as defense-in-depth Agent: code-health Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Agent: refactor/test-engineer Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…-suite flakiness The hetzner createServer resource-limit test and digitalocean OAuth recovery test used callCount-based mocks that broke when module state persisted across the full test suite. Switch to URL-based request matching so tests are isolated regardless of execution order. Agent: code-health Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
3a3224f to
c3eb653
Compare
la14-1
left a comment
There was a problem hiding this comment.
Test Engineer Review
Verdict: PR looks good and ready to merge.
Test Results
- main branch: 2186 pass, 2 fail (
digitalocean-token.test.ts,hetzner-cov.test.ts) — confirms issue #3393 - PR branch: 2188 pass, 0 fail across two consecutive runs — no flakiness observed
- Lint: Biome check clean (202 files, 0 errors)
Code Review
The fix addresses the root cause correctly with two complementary changes:
-
preload.ts— SetsSPAWN_TELEMETRY=0before tests, preventing telemetry's fire-and-forgetfetch()from pollutingglobal.fetchmocks in parallel test files. This is the right place for it since preload runs before any test file. -
telemetry.ts— Adds a runtime guard insendEvent()that re-checks test environment variables at send time. This is a good defense-in-depth measure against singleton state leaking. -
hetzner-cov.test.ts— Rewrites the count-based mock to URL-based routing. This is more robust because it doesn't depend on call ordering, which was the root cause of flakiness when telemetry injected extra fetch calls. -
digitalocean-token.test.ts— Same URL-based routing pattern. UsestoBeGreaterThanOrEqualfor call counts which is more tolerant of background fetch calls.
No issues found. Tests are clean, lint passes, and the approach is sound.
-- refactor/test-engineer
Why: Flaky test failures (2 tests fail non-deterministically in parallel execution) caused by telemetry's fire-and-forget
fetch()calls interfering with other test files'global.fetchmocks.Changes
preload.ts— SetsSPAWN_TELEMETRY=0before tests run, preventing telemetry from firing during test execution.telemetry.ts— Adds a runtime guard insendEvent()that re-checks the test environment, preventing stale singleton state from leaking fetch calls.Evidence
main: 2 flaky failures (hetzner-cov.test.ts,digitalocean-token.test.ts) that pass in isolation but fail in parallel runsSupersedes #3376 (same fix, rebased cleanly onto latest main).
Closes #3393
-- refactor/test-engineer